﻿using EA.BA;
using EA.Common;
using EA.DTO;
using EAWebAdmin.template;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EAAdmin
{
    public partial class EditCustomer : WebPage
    {
        override
        protected void Load(object sender, EventArgs e)
        {
            if (Request["id"] != null && !"".Equals(Request["id"]) && StringUtil.IsPositiveNumber(Request["id"]))
            {
                CustomerBA customerBA = new CustomerBA();
                int ID = Convert.ToInt32(Request["id"]);
                CustomerTO customer = customerBA.GetCustomerByID(ID);
                if (customer == null)
                {
                    this.error("Customer is invalid");
                    this.isLoad = true;
                    return;
                }
                Attributes.Add("customer", customer);
            }
            
        }
        public void Update(HttpRequest request, HttpResponse response)
        {
            if (Request["id"] == null || Request["id"].Equals("") || !StringUtil.IsPositiveNumber(Request["id"]))
            {
                this.error("Member is invalid");
                this.isLoad = true;
                return;
            }
            int ID = Convert.ToInt32(Request["id"]);
            CustomerBA customerBA = new CustomerBA();
            CustomerTO customer = customerBA.GetCustomerByID(ID);
            if (customer == null) 
            {
                this.error("Customer is invalid");
                this.isLoad = true;
                return;
            }
            String pPassword = Request.Form["pPassword"];
            String pFullName = Request.Form["pFullName"];
            String pAddress = Request.Form["pAddress"];
            String pPhone = Request.Form["pPhone"];
            int pSellable = Convert.ToInt32(Request.Form["pSellable"]);
            if (pPassword != null && pFullName != null && pAddress != null && pPhone != null)
            {
                Boolean temp = true;
                String err = "";               
                if ("".Equals(pPassword))
                {
                    err += "Password can not be empty<br>";
                    temp = false;
                }
                if ("".Equals(pFullName))
                {
                    err += "Full name can not be empty<br>";
                    temp = false;
                }
                if ("".Equals(pAddress))
                {
                    err += "Adress can not be empty<br>";
                    temp = false;
                }
                if ("".Equals(pPhone))
                {
                    err += "Phone can not be empty<br>";
                    temp = false;
                }
                            
                if (temp)
                {
                    Boolean stt = false;
                    customer.Password = Md5.md5(pPassword);
                    customer.FullName = pFullName;
                    customer.Sellable = pSellable;
                    customer.Phone = pPhone;
                    customer.Address = pAddress;
                    stt = customerBA.UpdateCustomer(customer);
                    if (stt == true)
                    {
                        Attributes.Add("success", "Update success");
                        Attributes.Add("customer", customer);
                        return;
                    }
                    else
                    {
                        Attributes.Add("err", "Update faulty");
                        return;
                    }
                }
                else
                {
                    Attributes.Add("err", err);
                    Attributes.Add("customer", customer);
                }
            }
        }
    }
}